<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Order extends CI_Controller
{
	public function __construct()
	{
		parent::__construct();
		
		$this->load->model('Shopcart_model','shopcart');	
		$this->load->model('Address_model','address');
		$this->load->model('Order_model','order');
	}
	
	public function create()
	{		
		$data = $this->shopcart->getAll();
		$areas = json_encode($this->address->getAreas());
		$address = false;
		if($user_id = $this->User_model->isLogin())
		{
			$address = $this->address->getDefaultOne($user_id);
		}
		$this->load->view('order/create',array('title'=>'创建订单 - 艾木 iMore.cc','data'=>$data,'areas'=>$areas,'address'=>$address));
	}
	
	public function save()
	{
		if(isset($_POST['email']))
		{
			$_POST['email'] = trim($_POST['email']);
			if(preg_match('#^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$#', $_POST['email']))
			{
				if($user = $this->User_model->simpleRegister($_POST['email']))
				{
					echo '电子邮箱：'.htmlentities($_POST['email'],ENT_QUOTES, "UTF-8");
				}else{
					//<a href="http://'.preg_replace('/[^@]*@(.*)/i','mail.$1',$_POST['email']).'"></a>
					echo '<font color="red">电子邮箱:'.htmlentities($_POST['email'], ENT_QUOTES, "UTF-8").',已经存在，请<b>登录</b>或接送邮箱验证邮件</font>';
				}
			}else{
				echo '<font color="red">电子邮箱:'.htmlentities($_POST['email'], ENT_QUOTES, "UTF-8").',输入不正确！</font>';
			}
		}
		elseif(isset($_POST['address']))
		{
			if($user = $this->User_model->isLogin())
			{
				$data = $this->input->post(NULL);
				if($addressinfo = $this->address->getAreasById($data['province'],$data['city'],$data['zone']))
				{
					$data['user_id'] = $user;
					$this->address->add($data);
					echo "收货地址：{$addressinfo}{$data['address']}<br />";
					if($data['zipcode'])
						echo "邮编：{$data['zipcode']}<br />";
					echo "收货人姓名：{$data['consignee']}<br />";
					if($data['mobile'])
						echo "手机：{$data['mobile']}<br />";
					if($data['telephone'])
						echo "固定电话：{$data['telephone']}<br />";
				}
				//Array ( [province] => 6 [city] => 77 [zone] => 707 [address] => 中山园路国际E城 [zipcode] => 421002 [consignee] => 罗崇 [mobile] => 15814099708 [telephone] => 3131591 )
			}
			else
			{
				echo 0;
			}
			
		}
	}
	
	public function payment()
	{
		if(isset($_GET['orderId']))
		{			
			$data = $this->order->getOrder(trim($_GET['orderId']));
			if(!$data || $data['user_id'] != $this->User_model->getUserId())
				show_404();			
		}
		else
		{
			$goods = $this->shopcart->getAll();
			$address = $this->address->getDefaultOne($this->User_model->getUserId());
			if(!$goods || !$address)
				show_404();
			$data = $this->order->insert($address,$goods);
			if($data)
			{
				$this->shopcart->delete();
				redirect('order/payment?orderId='.$data['order_sn']);
			}
		}
		$this->load->view('order/payment',array('title'=>'订单支付 - 艾木 iMore.cc','data'=>$data));
	}
	
	public function callpay()
	{
		if(isset($_GET['orderId']))
		{
			$data = $this->order->getOrder(trim($_GET['orderId']));
			$this->order->completePay($data['order_sn']);
			redirect('order/payment?orderId='.$data['order_sn']);
		}
		else
			show_404();
	}
	
	public function makefile()
	{		
	/*	$query = $this->db->query('SELECT * FROM '.$this->db->dbprefix('areas').' where parent_id = 0');
		$data = $query->result_array();
		$areas = array();
		foreach($data as $d)
		{
			$areas[$d['area_id']]['name'] = $d['area_name'];
			$query = $this->db->query('SELECT * FROM '.$this->db->dbprefix('areas').' where parent_id = '.$d['area_id']);
			$data1 = $query->result_array();
			foreach($data1 as $d1)
			{
				$areas[$d['area_id']]['c'][$d1['area_id']]['name'] = $d1['area_name'];
				$query = $this->db->query('SELECT * FROM '.$this->db->dbprefix('areas').' where parent_id = '.$d1['area_id']);
				$data2 = $query->result_array();
				foreach($data2 as $d2)
				{
					$areas[$d['area_id']]['c'][$d1['area_id']]['c'][$d2['area_id']]['name']  = $d2['area_name'];
					$query = $this->db->query('SELECT * FROM '.$this->db->dbprefix('areas').' where parent_id = '.$d2['area_id']);
					$data3 = $query->result_array();
					foreach($data3 as $d3)
					{
						$areas[$d['area_id']]['c'][$d1['area_id']]['c'][$d2['area_id']]['c'][$d3['area_id']]['name']  = $d3['area_name'];
					}
				}
			}
		}
				
		file_put_contents("./areas.php", "<?php\n \$areas=".var_export($areas,true).";\n?>");*/
	}
}

/* End of file Order.php */
/* Location: ./application/controllers/Order.php */